//
//  ViewController.m
//  xiaomaocuoyi
//
//  Created by 蔡舟 on 15/12/14.
//  Copyright © 2015年 com.xiaomaocuoyi. All rights reserved.
//

#import "ViewController.h"
#import "ShowPressHud.h"

@interface ViewController ()
{
    LoginService *service;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[ColorUtil colorWithHexString:@"#1b9ccd"]];
    [self configMainView];
    
    service = [[LoginService alloc]init];
    service.delegate = self;
}

-(void)configMainView{
    UIImageView *imgView = [[UIImageView alloc]init];
    [imgView setImage:[UIImage imageNamed:@"logo_login"]];
    [self.view addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).with.offset(20);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, SCREEN_HEIGHT*0.3));
    }];
    _phoneNumTxt = [[UITextField alloc]init];
    [_phoneNumTxt setBackgroundColor:[ColorUtil colorWithHexString:@"#ffffff"]];
    _phoneNumTxt.placeholder = @"手机号码";
    _phoneNumTxt.layer.cornerRadius = 5.f;
    [self.view addSubview:_phoneNumTxt];
    [_phoneNumTxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(20.f);
        make.top.equalTo(imgView.mas_bottom).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 35.f));
    }];
    UIButton *validateBtn = [UIButton new];
    [_phoneNumTxt addSubview:validateBtn];
    validateBtn.layer.cornerRadius = 5.f;
    [validateBtn setTitle:@"获取短信" forState:UIControlStateNormal];
    [validateBtn setTitleColor:[ColorUtil colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
    [validateBtn.titleLabel setFont:[UIFont systemFontOfSize:13.f]];
    [validateBtn setBackgroundColor:[UIColor blueColor]];
    [validateBtn addTarget:self action:@selector(validateBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [validateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_phoneNumTxt.mas_centerY);
        make.width.equalTo(@80);
        make.right.equalTo(_phoneNumTxt).with.offset(-5.f);
    }];
    
    
    _validateNumTxt = [[UITextField alloc]init];
    [_validateNumTxt setBackgroundColor:[ColorUtil colorWithHexString:@"#ffffff"]];
    _validateNumTxt.placeholder = @"验证码";
    _validateNumTxt.layer.cornerRadius = 5.f;
    [self.view addSubview:_validateNumTxt];
    [_validateNumTxt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(20.f);
        make.top.equalTo(_phoneNumTxt.mas_bottom).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 35.f));
    }];
    
    _loginBtn = [UIButton new];
    [self.view addSubview:_loginBtn];
    _loginBtn.layer.cornerRadius = 5.f;
    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_loginBtn setTitleColor:[ColorUtil colorWithHexString:@"#ffffff"] forState:UIControlStateNormal];
    [_loginBtn.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
    [_loginBtn setBackgroundColor:[UIColor orangeColor]];
    [_loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_validateNumTxt.mas_bottom).with.offset(20.f);
        make.left.equalTo(self.view).with.offset(20.f);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-40, 35.f));
    }];
}

-(void)loginBtnAction{
    NSString *phoneNum = _phoneNumTxt.text;
    NSString *validateNum = _validateNumTxt.text;
    if (phoneNum.length!=11) {
        [ShowPressHud showPressHudView:self.view andText:@"请输入正确的手机号码"];
    }else if (validateNum.length!=4){
        [ShowPressHud showPressHudView:self.view andText:@"请输入正确的验证码"];
    }else{
        NSMutableDictionary *tmpDic = [[NSMutableDictionary alloc]init];
        tmpDic[@"phone"] = phoneNum;
        tmpDic[@"validCode"] = validateNum;
        [service getLoginUser:tmpDic andView:self.view];
    }
    
}

-(void)getLoginUserSuccess:(NSString *)msg{
    
}

-(void)getLoginUserFail:(NSString *)msg{
    
}

-(void)getValidateNumSuccess{

}

-(void)getValidateNumFail{
    
}

-(void)validateBtnAction{
    NSLog(@"获取短信按钮");
    NSString *phoneNum = _phoneNumTxt.text;
    if (phoneNum.length!=11) {
        [ShowPressHud showPressHudView:self.view andText:@"请输入正确的手机号码"];
    }else{
        NSMutableDictionary *tmpDic = [[NSMutableDictionary alloc]init];
        tmpDic[@"phone"] = phoneNum;
        [service getValidateNum:tmpDic andView:self.view];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
